home *** CD-ROM | disk | FTP | other *** search
- /*
- Copyright © 1993-1997 Fabrizio Oddone
- ••• ••• ••• ••• ••• ••• ••• ••• ••• •••
- This source code is distributed as freeware:
- you may copy, exchange, modify this code.
- You may include this code in any kind of application: freeware,
- shareware, or commercial, provided that full credits are given.
- You may not sell or distribute this code for profit.
- */
-
-
- #include "UtilsSys7.h"
- #include "CursorBalloon.h"
- #include "FabACursors.h"
- #include "FabWList.h"
- #include "InitCursorBalloon.h"
- #include "SoundHandling.h"
- #include "Independents.h"
- #include "Internet.h"
- #include "FabLibResIDs.h"
- #include "SimResIDs.h"
-
- #include "Globals.h"
- #include "AEHandlers.h"
- #include "Animation.h"
- #include "ControlStore.h"
- #include "Disasm.h"
- #include "DoEditDialog.h"
- #include "DoMenu.h"
- #include "DragManSim.h"
- #include "Dump.h"
- #include "InitMenus.h"
- #include "Input.h"
- #include "Main.h"
- #include "Microprogram_Ed.h"
- #include "Registers.h"
- #include "SimAsm.h"
- #include "SimUtils.h"
- #include "GrowZone.h"
-
- #if defined(FabSystem7orlater)
-
- #pragma segment Main
-
- static AEEventHandlerUPP gmyHandleIO_UPP, gmyHandleGenericAlert_UPP;
-
- #pragma segment Init
-
- static void Init_My_Menus(void);
- static void GestaltCheck(void);
- static Boolean InitPrefs(void);
- static OSErr InitIO(void);
- static void FatalErrorAlert(short alertID, OSErr reason);
-
- /* Init_My_Menus: sets up the menu bar */
-
- static void Init_My_Menus(void)
- {
- {
- register Handle menuBar;
-
- SetMenuBar(menuBar = GetNewMBar(kRes_Menu_Apple));
- DisposeHandle(menuBar);
- }
-
- AppendResMenu(gMenu_Apple = GetMenuHandle(kRes_Menu_Apple),'DRVR');
- gMenu_File = GetMenuHandle(kRes_Menu_File);
- gMenu_Edit = GetMenuHandle(kRes_Menu_Edit);
- gMenu_Windows = GetMenuHandle(kRes_Menu_Windows);
- gMenu_Control = GetMenuHandle(kRes_Menu_Control);
- gMenu_Assembler = GetMenuHandle(kRes_Menu_Assembler);
- InsertMenu(GetMenu(kRes_Menu_HMemory), hierMenu);
- InsertMenu(GetMenu(kRes_Menu_HRegs), hierMenu);
- InsertMenu(GetMenu(kRes_Menu_HDisasm), hierMenu);
- InsertMenu(GetMenu(kRes_Menu_HDump), hierMenu);
- InsertMenu(GetMenu(kMENU_HCopyWebLocation), hierMenu);
- InsertMenu(GetMenu(kMENU_HVisitWebLocation), hierMenu);
- gPopMenu = GetMenu(kRes_Menu_PopDump);
- //DrawMenuBar();
- }
-
- /* InitAll: initializes the whole environment */
-
- void InitAll(void)
- {
- EventRecord dummyEvent;
- Rect startingRect, endingRect;
- Point dummyPt = {0, 0};
- DialogPtr agh;
- Handle splash;
- OSErr err;
- Boolean prefsFileWasAbsent;
-
- MoreMasters();
- MoreMasters();
- MoreMasters();
- MoreMasters();
- MoreMasters();
- MoreMasters();
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
-
- FlushEvents(everyEvent, 0);
- InitCursor();
-
- (void)UnloadScrap();
-
- GestaltCheck();
-
- gWatchHandle = GetCursor(watchCursor);
- SetCursor(*gWatchHandle);
- (void) FabLoadCursors(128);
-
- InitMySoundHandling();
-
- gInstrClikLoopUPP = MyNewListClickLoopProc(instrClikLoop);
- gCommentClikLoopUPP = MyNewListClickLoopProc(commentClikLoop);
- gSwitchCursClikLoopUPP = MyNewListClickLoopProc(switchCursClikLoop);
-
- (void)InitCursorBalloonManager();
-
- splash = OpenSplash(&agh, nil);
- SetGrafPortOfDialog(agh);
- endingRect = agh->portRect;
- LocalToGlobal(&topLeft(endingRect));
- LocalToGlobal(&botRight(endingRect));
- startingRect = endingRect;
- InsetRect(&startingRect, ((endingRect.right - endingRect.left) >> 1) - 2,
- ((endingRect.bottom - endingRect.top) >> 1) - 2);
-
- FabRotateCursor();
-
- ZoomingRects(&gPrefs.zfr, &startingRect, &endingRect, zoomAccelerate);
- ShowWindow(agh);
- DrawDialog(agh);
-
- gIBeamHandle = GetCursor(iBeamCursor);
- gPlusHandle = GetCursor(plusCursor);
-
- FabRotateCursor();
-
- gMMemory = NewPtrClear(kSIZE_RAM);
- gCsMemory = (union u_mir *)NewPtrClear(kSIZE_ASSMEM + kSIZE_CSMEM);
- if (gCsMemory)
- gAssMemory = (((Ptr)gCsMemory) + kSIZE_CSMEM);
-
- MyFillBuffer((long *)gAssMemory, numOfLongs(kSIZE_ASSMEM), 0x03030303);
-
- FabRotateCursor();
-
- Init_My_Menus();
-
- gRegs[kREG_ZERO] = 0;
- gRegs[kREG_ONE] = 1;
- gRegs[kREG_MINUS1] = -1;
-
- FabRotateCursor();
-
- prefsFileWasAbsent = InitPrefs();
-
- FabRotateCursor();
-
- ResetMemory();
- ResetRegisters();
- UnloadSeg(ResetRegisters);
-
- FabRotateCursor();
-
- if (Init_Animation()) ExitToShell();
-
- FabRotateCursor();
-
- if (Init_Microprogram_Ed()) ExitToShell();
-
- FabRotateCursor();
-
- if (Init_Registers()) ExitToShell();
-
- FabRotateCursor();
-
- if (InitIO()) ExitToShell();
-
- FabRotateCursor();
-
- if (InitDump()) ExitToShell();
-
- FabRotateCursor();
-
- if (Init_Disasm()) ExitToShell();
-
-
- SetMir(-1L);
- FabRotateCursor();
- SetMir(0L);
-
- if (prefsFileWasAbsent)
- gPrefs.remembWind = true;
-
- if(err = InstallRequiredAEHandlers(myHandleOAPP, myHandleODOC, myHandlePDOC, myHandleQUIT)) {
- FatalErrorAlert(kALRT_GENERICERROR, err);
- gDoneFlag = true;
- }
-
- FabRotateCursor();
-
- gmyHandleIO_UPP = NewAEEventHandlerProc(myHandleIO);
- gmyHandleGenericAlert_UPP = NewAEEventHandlerProc(myHandleGenericAlert);
- if(err = AEInstallEventHandler(kFCR_MINE, kAEmySignalIO, gmyHandleIO_UPP, 0, false)) {
- FatalErrorAlert(kALRT_GENERICERROR, err);
- gDoneFlag = true;
- }
-
- if(err = AEInstallEventHandler(kCreat, kAEAlert, gmyHandleGenericAlert_UPP, 0, false)) {
- FatalErrorAlert(kALRT_GENERICERROR, err);
- gDoneFlag = true;
- }
-
- (void)TEFromScrap();
-
- FabRotateCursor();
-
- gICerr = MyICInit(&gICinst, kFCR_MINE);
- UnloadSeg(ICStart);
-
- FabRotateCursor();
-
- DisposeSplash(splash, agh);
- UnloadSeg(DisposeSplash);
-
- FabRotateCursor();
-
- (void) EventAvail(0, &dummyEvent);
-
- FabRotateCursor();
-
- if (gPrefs.AnimVisible)
- ShowWindowAndCheckMenu(gWPtr_Animation, kMItem_Animation);
- if (gPrefs.DisasmVisible) {
- DecideActivation(&dummyEvent, gWPtr_Disasm, dummyPt, false);
- ShowWindowAndCheckMenu(gWPtr_Disasm, kMItem_Disasm);
- }
- if (gPrefs.DumpVisible) {
- DecideActivation(&dummyEvent, gWPtr_Dump, dummyPt, false);
- ShowWindowAndCheckMenu(gWPtr_Dump, kMItem_Dump);
- }
- if (gPrefs.IOVisible) {
- DecideActivation(&dummyEvent, gWPtr_IO, dummyPt, false);
- ShowWindowAndCheckMenu(gWPtr_IO, kMItem_IO);
- }
- if (gPrefs.RegsVisible)
- ShowWindowAndCheckMenu(gWPtr_Registers, kMItem_Registers);
-
- gILCBase = gRegs[kREG_PC];
-
- FabRotateCursor();
-
- AdjustMenus(nil);
- DrawMenuBar();
- FabFreeCursors();
- InitGrowZone();
- InitCursor();
- }
-
- /* GestaltCheck: is this Mac set up with all we need to run? */
-
- static void GestaltCheck(void)
- {
- #define kMinimumMemoryFree (236L*1024)
-
- #define POWERMANAGER ((1L << gestaltPMgrExists)|(1L << gestaltPMgrCPUIdle))
- #define TEMPMEMMANAGER ((1L << gestaltTempMemSupport)|(1L << gestaltRealTempMemory)|(1L << gestaltTempMemTracked))
- long Gresp;
- unsigned long totalSize, contigSize;
-
- PurgeSpace((long *)&totalSize, (long *)&contigSize);
- if (totalSize < kMinimumMemoryFree) {
- (void)StopAlert_UPP(kALRT_MEMEXHAUSTED, nil);
- ExitToShell();
- }
-
- if (TrapAvailable(_Gestalt)) {
- gDragManagerActive = HasDragManager();
- if (Gestalt(gestaltQuickdrawVersion, &Gresp) == noErr)
- gHasColorQD = ((Byte)(Gresp >> 8)) > 0;
- if (Gestalt(gestaltPowerMgrAttr, &Gresp) == noErr)
- gPwrManagerIsPresent = ((Gresp & POWERMANAGER) == POWERMANAGER) != 0;
- if (Gestalt(gestaltAliasMgrAttr, &Gresp) == noErr)
- if (Gresp & (1L << gestaltAliasMgrPresent))
- if (Gestalt(gestaltAppleEventsAttr, &Gresp) == noErr)
- if (Gresp & (1L << gestaltAppleEventsPresent))
- if (Gestalt(gestaltDITLExtAttr, &Gresp) == noErr)
- if (Gresp & (1L << gestaltDITLExtPresent))
- if (Gestalt(gestaltFindFolderAttr, &Gresp) == noErr)
- if (Gresp & (1L << gestaltFindFolderPresent))
- if (Gestalt(gestaltFSAttr, &Gresp) == noErr)
- if (Gresp & (1L << gestaltHasFSSpecCalls))
- if (Gestalt(gestaltHelpMgrAttr, &Gresp) == noErr)
- if (Gresp & (1L << gestaltHelpMgrPresent))
- if (Gestalt(gestaltPopupAttr, &Gresp) == noErr)
- if (Gresp & (1L << gestaltPopupPresent))
- // System 7.0 bug: it doesn't install the gestaltResourceMgrAttr selector
- // if (Gestalt(gestaltResourceMgrAttr, &Gresp) == noErr)
- // if (Gresp & (1L << gestaltPartialRsrcs))
- if (Gestalt(gestaltStandardFileAttr, &Gresp) == noErr)
- if (Gresp & (1L << gestaltStandardFile58))
- if (Gestalt(gestaltTextEditVersion, &Gresp) == noErr)
- if (Gresp >= gestaltTE4)
- if (Gestalt(gestaltOSAttr, &Gresp) == noErr)
- if ((Gresp & TEMPMEMMANAGER) == TEMPMEMMANAGER)
- /* proceed because everything is OK */
- return;
- }
- (void)StopAlert_UPP(kALRT_OLDSYSTEM, nil);
- ExitToShell();
- }
-
-
- /* InitPrefs: loads preferences if present */
-
- Boolean InitPrefs(void)
- {
- ParamBlockRec myPB;
- EventRecord dummyEv;
- FSSpec myFSS;
- Handle myStrHand, prefsBuffer;
- short prefsFRefNum;
- Boolean targetFolder, isAnAlias;
- OSErr err;
- SignedByte oldState;
- Boolean prefsFileAbsent = false;
-
- oldState = WantThisHandleSafe(myStrHand = (Handle)GetString(kSTR_PREFSFILENAME));
- if ((err = FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
- &myFSS.vRefNum, &myFSS.parID)) == noErr) {
- err = FSMakeFSSpecCompat(myFSS.vRefNum, myFSS.parID, (ConstStr255Param)*myStrHand, &myFSS);
- if (err == fnfErr)
- prefsFileAbsent = true;
- else if (err == noErr)
- if ((err = ResolveAliasFile(&myFSS, true, &targetFolder, &isAnAlias)) == noErr)
- if (targetFolder)
- err = paramErr;
- else
- if ((err = FSpOpenDFCompat(&myFSS, fsRdPerm, &prefsFRefNum)) == noErr) {
- if (prefsBuffer = NewHandleGeneral(sizeof(struct myprefs))) {
- myPB.ioParam.ioCompletion = nil;
- myPB.ioParam.ioRefNum = prefsFRefNum;
- HLockHi(prefsBuffer);
- myPB.ioParam.ioBuffer = *prefsBuffer;
- myPB.ioParam.ioReqCount = sizeof(struct myprefs);
- myPB.ioParam.ioPosMode = fsFromStart | kNoCacheMask;
- myPB.ioParam.ioPosOffset = 0L;
- (void)PBReadAsync(&myPB);
- while (myPB.ioParam.ioResult > 0) {
- SystemTask();
- (void)EventAvail(everyEvent, &dummyEv);
- }
- if ((err = myPB.ioParam.ioResult) == noErr)
- if (((struct myprefs *)*prefsBuffer)->prefsVersion == kCurPrefsVersion) {
- gPrefs = *(struct myprefs *)*prefsBuffer;
- }
- else if (((struct myprefs *)*prefsBuffer)->prefsVersion > kCurPrefsVersion) {
- gDoNotOverwritePrefs = gDoneFlag = CautionAlert_UPP(kALRT_PREFSWARNING, nil) == cancel;
- SetCursor(*gWatchHandle);
- }
- DisposeHandle(prefsBuffer);
- }
- (void)FSClose(prefsFRefNum);
- }
- }
- HSetState(myStrHand, oldState);
-
- return prefsFileAbsent;
- }
-
- /* InitIO: initializes the input/output window */
-
- static OSErr InitIO(void)
- {
- Rect destRect, viewRect;
- FabWindowPtr thefabw;
- register WindowPtr window;
- register DocumentIOPeek doc;
- register OSErr err = appMemFullErr;
-
- gWPtr_IO = window = GetNewWindow(kWIND_IO, nil, (WindowPtr)-1L);
- if (window) {
- SetPortWindowPort(window);
-
- if (gPrefs.remembWind) {
- if (IsOnScreen(&gPrefs.IOUserState)) {
- (*(WStateDataHandle)((WindowPeek)window)->dataHandle)->userState = gPrefs.IOUserState;
- ZoomWindow(window, inZoomIn, false);
- }
- }
- doc = &gIODoc;
- GetTERect(window, &viewRect);
- destRect = viewRect;
- destRect.right = destRect.left +
- (qd.screenBits.bounds.right - qd.screenBits.bounds.left);
- if ( doc->docTE = TENew(&destRect, &viewRect) ) {
- (void)TEFeatureFlag(teFOutlineHilite, teBitSet, doc->docTE);
- TEAutoView(true, doc->docTE);
- if (doc->docVScroll = GetNewControl(kCNTL_IOVScroll, window)) {
- if (doc->docHScroll = GetNewControl(kCNTL_IOHScroll, window)) {
- // adjust & draw the controls, draw the window
- AdjustScrollbars(window, true);
-
- thefabw = AddWindowToList(window);
- doc->docWindow = thefabw;
- SetActivate(thefabw, DoActivateWindow);
- SetUpdate(thefabw, DoUpdateWindow);
- SetGrow(thefabw, DoGrowWindow);
- SetZoom(thefabw, DoZoomWindow);
- SetGoAway(thefabw, CloseIO);
- SetContent(thefabw, DoContentClick);
-
- InstallRgnHandler(thefabw, NewRgn(), RecalcIO, gIBeamHandle,
- toMenu(kBalloons_Dump, kBIO_Contents), 0, 0);
- ResizeObjects(window);
- RecalcGlobalCoords(thefabw);
-
- err = noErr;
- }
- }
- }
- }
- return err;
- } /* InitIO */
-
- /* FatalErrorAlert: errors while initing windows */
-
- static void FatalErrorAlert(short alertID, OSErr reason)
- {
- Str255 tempS;
-
- MyNumToString(reason, tempS);
- ParamText(tempS, nil, nil, nil);
- (void)StopAlert_UPP(alertID, nil);
- }
-
- #endif
-
-